﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using Sunny.UI;
using System;
using System.Data;

namespace MesBSW.View.Dialog.Role
{
    public partial class AddUserRoleDia : UIEditForm
    {

        private RoleInfoControler _my_ctrl;
        private DataTable dt;
        private int user_id;


        public AddUserRoleDia()
        {
            InitializeComponent();
        }

        public AddUserRoleDia(int user_id, RoleInfoControler _my_ctrl)
        {
            InitializeComponent();
            this._my_ctrl = _my_ctrl;
            this.user_id = user_id;
            dt = this._my_ctrl.getUserRoles(user_id);
            InitComboBoxRole();
            InitDiaValue();
        }


        private void InitDiaValue() 
        {
            this.user_name.Text = dt.Rows[0]["user_name"].ToString();
            this.real_name.Text = dt.Rows[0]["real_name"].ToString();
            ComboBoxRole.SelectedValue = dt.Rows[0]["base_role_id"].ToString();
            ComboBoxRole.Text = dt.Rows[0]["role_name"].ToString();
        }

        private void InitComboBoxRole() 
        {
            ComboBoxRole.ValueMember = "id";
            ComboBoxRole.DisplayMember = "role_name";
            ComboBoxRole.DataSource = this._my_ctrl.getRoles().ToDataTable();
        }


        protected override bool CheckData()
        {
            return CheckEmpty(ComboBoxRole, "请选择角色");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ComboBoxRole.Text != "") 
            {
                BaseUserRoleModel userRoleModel = new BaseUserRoleModel();

                userRoleModel.base_role_id = ComboBoxRole.SelectedValue.ToString().ToInt();
                userRoleModel.base_user_id = this.user_id;

                _my_ctrl.saveUserRole(userRoleModel);
                UIMessageBox.Show(SysTools.errMsg);
            }
         
        }
    }
    
}
